package cn.mania_man.trpg_helper_server.util;

import cn.mania_man.trpg_helper_server.room.RequestMessage;
import cn.mania_man.trpg_helper_server.room.ResponseMessage;
import cn.mania_man.trpg_helper_server.room.Room;
import cn.mania_man.trpg_helper_server.websocket.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.stream.Collectors;

public class ResponseUtil {
    public static ResponseMessage massage(String body, String receiver, Room room) {
        ResponseMessage res = new ResponseMessage();
        res.setType(ResTypeEnum.message);
        res.addSender("server");
        res.addReceiver(receiver, room, true);
        res.setBody(body);
        return res;
    }

    public static ResponseMessage forward(RequestMessage req, User sender, ResTypeEnum resType) {
        ResponseMessage res = new ResponseMessage(req.getBody(), sender, resType);
        res.addReceiver(req.getReceiver(), sender.getRoom(), true);
        res.ignoreUser(sender);
        return res;
    }

    public static ResponseMessage userList(Room room) {
        ResponseMessage res = new ResponseMessage();
        res.setType(ResTypeEnum.send_user_list);
        res.addSender("server");
        res.addReceiver("*",room,true);
        String body = JSON.toJSONString(room.getUsers().stream().map(User::getUserName).collect(Collectors.toSet()));
        res.setBody(body);
        return  res;
    }

}
